package net.fltx.taskmgr.win32api;

import com.sun.jna.Native;
import com.sun.jna.Structure;
import com.sun.jna.WString;
import jdk.nashorn.internal.objects.NativeInt8Array;

import java.io.UnsupportedEncodingException;
import java.util.Arrays;
import java.util.List;

/**
 * ProcessEntry32结构体
 */
public class ProcessEntry32 extends Structure {
    public static class ByReference extends ProcessEntry32 implements Structure.ByReference { }
    public static class ByValue extends ProcessEntry32 implements Structure.ByValue{}
    public int dwSize=1060;//结构的大小
    public int cntUsage; //此进程的引用计数
    public int th32ProcessID;//进程ID
    public long th32DefaultHeapID;//进程默认堆ID
    public int th32ModuleID;//进程模块ID
    public int cntThreads;//此进程开启的线程计数
    public int th32ParentProcessID;//父进程的ID
    public int pcPriClassBase;//线程优先权
    public int dwFlags;//结构体标识,这个成员已经不再被使用设置为零。
    public byte[] szExeFile=new byte[1024];//进程的可执行文件名称。要获得可执行文件的完整路径，应调用Module32First函数

    @Override
    public List<String> getFieldOrder() {
        return Arrays.asList(new String[]{"dwSize", "cntUsage", "th32ProcessID", "th32DefaultHeapID",
                "th32ModuleID", "cntThreads","th32ParentProcessID","pcPriClassBase","dwFlags","szExeFile"});
    }

    public String getSzExeFile() {
        String ExeFile="";
        try {
            ExeFile=(new String(szExeFile,"gbk")).trim();
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
        return ExeFile;
    }

    public ProcessEntry32 free(){
        szExeFile=new byte[1024];
        return this;
    }

    @Override
    public String toString() {
        String ExeFile="";
        try {
            ExeFile=(new String(szExeFile,"gbk")).trim();
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
        return "ProcessEntry32{" +
                "dwSize=" + dwSize +
                ", cntUsage=" + cntUsage +
                ", th32ProcessID=" + th32ProcessID +
                ", th32DefaultHeapID=" + th32DefaultHeapID +
                ", th32ModuleID=" + th32ModuleID +
                ", cntThreads=" + cntThreads +
                ", th32ParentProcessID=" + th32ParentProcessID +
                ", pcPriClassBase=" + pcPriClassBase +
                ", dwFlags=" + dwFlags +
                ", szExeFile=" + ExeFile +
                '}';
    }
}
